<?php

class Grillas_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    private $page;
    private $limit;
    private $sidx;
    private $sord;
    private $wh;
    private $total_pages;
    private $count;
    private $start;
    

    private function Strip($value) {
        if (get_magic_quotes_gpc() != 0) {
            if (is_array($value))
                if ($this->array_is_associative($value)) {
                    foreach ($value as $k => $v)
                        $tmp_val[$k] = stripslashes($v);
                    $value = $tmp_val;
                }
                else
                    for ($j = 0; $j < sizeof($value); $j++)
                        $value[$j] = stripslashes($value[$j]);
            else
                $value = stripslashes($value);
        }
        return $value;
    }

    private function getStringForGroup($group) {
        $i_ = '';
        $sopt = array('eq' => "=", 'ne' => "<>", 'lt' => "<", 'le' => "<=", 'gt' => ">", 'ge' => ">=", 'bw' => " {$i_}LIKE ", 'bn' => " NOT {$i_}LIKE ", 'in' => ' IN ', 'ni' => ' NOT IN', 'ew' => " {$i_}LIKE ", 'en' => " NOT {$i_}LIKE ", 'cn' => " {$i_}LIKE ", 'nc' => " NOT {$i_}LIKE ", 'nu' => 'IS NULL', 'nn' => 'IS NOT NULL');
        $s = "(";
        if (isset($group['groups']) && is_array($group['groups']) && count($group['groups']) > 0) {
            for ($j = 0; $j < count($group['groups']); $j++) {
                if (strlen($s) > 1) {
                    $s .= " " . $group['groupOp'] . " ";
                }
                try {
                    $dat = $this->getStringForGroup($group['groups'][$j]);
                    $s .= $dat;
                } catch (Exception $e) {
                    echo $e->getMessage();
                }
            }
        }
        if (isset($group['rules']) && count($group['rules']) > 0) {
            try {
                foreach ($group['rules'] as $key => $val) {
                    if (strlen($s) > 1) {
                        $s .= " " . $group['groupOp'] . " ";
                    }
                    $field = $val['field'];
                    $op = $val['op'];
                    $v = $val['data'];
                    if ($op) {
                        switch ($op) {
                            case 'bw':
                            case 'bn':
                                $s .= $field . ' ' . $sopt[$op] . "'$v%'";
                                break;
                            case 'ew':
                            case 'en':
                                $s .= $field . ' ' . $sopt[$op] . "'%$v'";
                                break;
                            case 'cn':
                            case 'nc':
                                $s .= $field . ' ' . $sopt[$op] . "'%$v%'";
                                break;
                            case 'in':
                            case 'ni':
                                $s .= $field . ' ' . $sopt[$op] . "( '$v' )";
                                break;
                            case 'nu':
                            case 'nn':
                                $s .= $field . ' ' . $sopt[$op] . " ";
                                break;
                            default :
                                $s .= $field . ' ' . $sopt[$op] . " '$v' ";
                                break;
                        }
                    }
                }
            } catch (Exception $e) {
                echo $e->getMessage();
            }
        }
        $s .= ")";
        if ($s == "()") {
            //return array("",$prm); // ignore groups that don't have rules
            return " 1=1 ";
        } else {
            return $s;
            ;
        }
    }

//fin private function getStringForGroup($group){

    private function array_is_associative($array) {
        if (is_array($array) && !empty($array)) {
            for ($iterator = count($array) - 1; $iterator; $iterator--) {
                if (!array_key_exists($iterator, $array)) {
                    return true;
                }
            }
            return !array_key_exists(0, $array);
        }
        return false;
    }

    private function cargaParametrosGrillaIniciales() {
        $this->page = $this->input->post('page');
        $this->limit = $this->input->post('rows');
        $this->sidx = ($this->input->post('sidx')) ? $this->input->post('sidx') : 1;
        $this->sord = $this->input->post('sord');
        //Parametros de búsqueda
        $this->wh = "";
        $searchOn = $this->Strip($_REQUEST['_search']);
        if ($searchOn == 'true') {
            $searchstr = $this->Strip($_REQUEST['filters']);
            $jsona = json_decode($searchstr, true);
            $this->wh = " AND " . $this->getStringForGroup($jsona);
        }
    } //fin private function cargaParametrosGrillaIniciales() {
    
    private function totalesGrilla($count){
        $this->cuont = $count;
        if ($count > 0)
            $this->total_pages = ceil($count / $this->limit);
        else
            $this->total_pages = 0;
        if ($this->page > $this->total_pages)
            $this->page = $this->total_pages;
        $this->start = $this->limit * $this->page - $this->limit; // do not put $limit*($page - 1)
        $this->start = ($this->start > 0) ? $this->start : 0;
    }

    public function topografias($transaccion) {
        //Variables del jqgrid
        $this->cargaParametrosGrillaIniciales();
        $sql = "select count(*) as total from temporal tmp 
                inner join unidades und on und.unidadId = tmp.temporalInt1 where tmp.transaccion = '$transaccion' " . $this->wh;
        $query = $this->db->query($sql);
        $obj = $query->result();
        $this->totalesGrilla($obj[0]->total);
        $sql = "select tmp.temporalId,tmp.temporalVar1,tmp.temporalDecimal1,und.unidadNombre,tmp.temporalVar2 from temporal tmp
                inner join unidades und on und.unidadId = tmp.temporalInt1 where tmp.transaccion = '$transaccion' " . $this->wh . " ORDER BY " . $this->sidx . " " . $this->sord . " LIMIT " . $this->start . " , " . $this->limit;
        $query = $this->db->query($sql);
        $responce = new stdClass();
        $responce->page = $this->page;
        $responce->total = $this->total_pages;
        $responce->records = $this->cuont;
        $i = 0;
        foreach ($query->result() as $value) {
            $responce->rows[$i]['id'] = $value->temporalId;
            $responce->rows[$i]['cell'] = array($value->temporalVar1, $value->temporalDecimal1, $value->unidadNombre,$value->temporalVar2);
            $i++;
        }
        echo json_encode($responce);
    }//fin public function topografias($transaccion) {
    public function construcciones($transaccion) {
        //Variables del jqgrid
        $this->cargaParametrosGrillaIniciales();
        $innerJoin = "inner join unidades und on und.unidadId = tmp.temporalInt1 
                    inner join estadosPredios esta on esta.estadoPredId = tmp.temporalInt2 ";
        $sql = "select count(*) as total from temporal tmp 
                $innerJoin
                where tmp.transaccion = '$transaccion' " . $this->wh;
        $obj = $this->db->query($sql)->result();
        $this->totalesGrilla($obj[0]->total);
        $sql = "select tmp.temporalId,tmp.temporalVar1,tmp.temporalDecimal1,und.unidadNombre,esta.estadoPredNombre,tmp.temporalInt3,tmp.temporalText1 from temporal tmp
                $innerJoin
                where tmp.transaccion = '$transaccion' " . $this->wh . " ORDER BY " . $this->sidx . " " . $this->sord . " LIMIT " . $this->start . " , " . $this->limit;
        $query = $this->db->query($sql);
        $responce = new stdClass();
        $responce->page = $this->page;
        $responce->total = $this->total_pages;
        $responce->records = $this->cuont;
        $i = 0;
        foreach ($query->result() as $value) {
            $responce->rows[$i]['id'] = $value->temporalId;
            $responce->rows[$i]['cell'] = array($value->temporalVar1, $value->temporalDecimal1, $value->unidadNombre,$value->estadoPredNombre,$value->temporalInt3,$value->temporalText1);
            $i++;
        }
        echo json_encode($responce);
    }//fin public function topografias($transaccion) {
    public function elementos($transaccion) {
        //Variables del jqgrid
        $this->cargaParametrosGrillaIniciales();
        $inner = " inner join unidades u on u.unidadId = tmp.temporalInt2 ";
        $sql = "select count(*) as total from temporal tmp $inner
                where tmp.transaccion = '$transaccion' " . $this->wh;
        $obj = $this->db->query($sql)->result();
        $this->totalesGrilla($obj[0]->total);
        $sql = "select tmp.temporalId,tmp.temporalVar1,tmp.temporalDecimal1,tmp.temporalInt1,tmp.temporalDecimal2,u.unidadNombre from temporal tmp
                $inner
                where tmp.transaccion = '$transaccion' " . $this->wh . " ORDER BY " . $this->sidx . " " . $this->sord . " LIMIT " . $this->start . " , " . $this->limit;
        $query = $this->db->query($sql);
        $responce = new stdClass();
        $responce->page = $this->page;
        $responce->total = $this->total_pages;
        $responce->records = $this->cuont;
        $i = 0;
        foreach ($query->result() as $value) {
            $responce->rows[$i]['id'] = $value->temporalId;
            $responce->rows[$i]['cell'] = array($value->temporalVar1,$value->unidadNombre,$value->temporalInt1, $value->temporalDecimal1, $value->temporalDecimal2);
            $i++;
        }
        echo json_encode($responce);
    }//fin public function elementos($transaccion) {
    public function fotos($avaluos){
        //Variables del jqgrid
        $this->cargaParametrosGrillaIniciales();
        $sql = "select count(*) as total from fotosVisitas where visitaId = '$avaluos' ". $this->wh;
        $obj = $this->db->query($sql)->result();
        $this->totalesGrilla($obj[0]->total);
        $sql = "select fotoVisitaId,fotoVisitaNombre,fotoVisitaOrden,fotoVisitaRuta,fotoVisitaPortada from fotosVisitas 
                where visitaId = '$avaluos'  " . $this->wh. " ORDER BY " . $this->sidx . " " . $this->sord . " LIMIT " . $this->start . " , " . $this->limit;
        $query = $this->db->query($sql);
        $responce = new stdClass();
        $responce->page = $this->page;
        $responce->total = $this->total_pages;
        $responce->records = $this->cuont;
        $i = 0;
        foreach ($query->result() as $value) {
            $responce->rows[$i]['id'] = $value->fotoVisitaId;
            $responce->rows[$i]['cell'] = array('',$value->fotoVisitaNombre,$value->fotoVisitaOrden,$value->fotoVisitaPortada,$value->fotoVisitaRuta);
            $i++;
        }
        echo json_encode($responce);
    } //fin public function fotos($avaluos){
    public function visitas() {        
        //Variables del jqgrid
        $this->cargaParametrosGrillaIniciales();
        if(trim($this->input->post("visitaFechaVisitaIni")) == "" && trim($this->input->post("visitaFechaVisitaFin")) == "" && trim($this->input->post("avaluoId")) == "" && trim($this->input->post("ciudadId")) == "" && trim($this->input->post("visitaVereda")) == "" && trim($this->input->post("visitaVeredaNombre")) == "" && trim($this->input->post("tipInmuebleId")) == "" && trim($this->input->post("visitaPropietario")) == "" && trim($this->input->post("firmaId")) == "" && trim($this->input->post("selDepartamento")) == ""){
            exit();
        }
        $fecIni = ($this->input->post("visitaFechaVisitaIni")) ? $this->input->post("visitaFechaVisitaIni"):"1986-10-01";
        $fecFin = ($this->input->post("visitaFechaVisitaFin")) ? $this->input->post("visitaFechaVisitaFin"):"2986-10-01";
        $where = "";
        $where .= ($this->input->post("avaluoId")) ? " and visi.avaluoId = {$this->input->post("avaluoId")}":"";
        $where .= ($this->input->post("ciudadId")) ? " and visi.ciudadId = {$this->input->post("ciudadId")}":"";
        $where .= ($this->input->post("visitaVereda")) ? " and visi.visitaVereda like '%{$this->input->post("visitaVereda")}%'":"";
        $where .= ($this->input->post("visitaVeredaNombre")) ? " and visi.visitaVeredaNombre like '%{$this->input->post("visitaVeredaNombre")}%'":"";
        $where .= ($this->input->post("tipInmuebleId")) ? " and visi.tipInmuebleId = {$this->input->post("tipInmuebleId")}":"";
        $where .= ($this->input->post("visitaPropietario")) ? " and visi.visitaPropietario like '%{$this->input->post("visitaPropietario")}%'":"";
        $where .= ($this->input->post("firmaId")) ? " and visi.firmaId = {$this->input->post("firmaId")}":"";
        $whereDep = ($this->input->post("selDepartamento")) ? " and dep.dptoId = {$this->input->post("selDepartamento")}":"";
        $innerJoin = "inner join ciudades ciu on ciu.ciudadId = visi.ciudadId
                inner join avaluos ava on ava.avaluoId = visi.avaluoId
                inner join tiposInmuebles tip on tip.tipInmuebleId = visi.tipInmuebleId
                inner join departamentos dep on dep.dptoId = ciu.dptoId $whereDep";
        $sql = "select count(*) as total from visitas visi
                $innerJoin
                where visi.visitaFechaVisita between '$fecIni' and '$fecFin'  $where " . $this->wh;
        $obj = $this->db->query($sql)->result();
        $this->totalesGrilla($obj[0]->total);
        $sql = "select visi.visitaId,visi.visitaFechaVisita, ava.avaluoNumero, visi.visitaPropietario,dep.dptoNombre,ciu.ciudadNombre,visi.visitaVereda,visi.visitaVeredaNombre,tip.tipInmuebleNombre from visitas visi
                $innerJoin
                where visi.visitaFechaVisita between '$fecIni' and '$fecFin'  $where " . $this->wh. " ORDER BY " . $this->sidx . " " . $this->sord . " LIMIT " . $this->start . " , " . $this->limit;
        $query = $this->db->query($sql);
        $responce = new stdClass();
        $responce->page = $this->page;
        $responce->total = $this->total_pages;
        $responce->records = $this->cuont;
        $i = 0;
        foreach ($query->result() as $value) {
            $responce->rows[$i]['id'] = $value->visitaId;
            $responce->rows[$i]['cell'] = array('',$value->visitaFechaVisita,$value->avaluoNumero, $value->visitaPropietario, $value->dptoNombre, $value->ciudadNombre, $value->visitaVereda, $value->visitaVeredaNombre, $value->tipInmuebleNombre);
            $i++;
        }
        echo json_encode($responce);
    }//fin public function visitas() {
    public function proyectos() {
        //Variables del jqgrid
        $this->cargaParametrosGrillaIniciales();
        $where = "where proyectoEstadoSistema = 'A'  " . $this->wh;
        $sql = "select count(*) as total from proyectos ".$where;
        $obj = $this->db->query($sql)->result();
        $this->totalesGrilla($obj[0]->total);
        $sql = "select proyectoId,proyectoContrato,proyectoNombre,proyectoDirector,proyectoCelular,proyectoTelefono,proyectoDireccion,proyectoNumAvaluos,proyectoEstado,proyectoObservacion  from proyectos $where ORDER BY " . $this->sidx . " " . $this->sord . " LIMIT " . $this->start . " , " . $this->limit;
        $responce = new stdClass();
        $query = $this->db->query($sql);
        $responce->page = $this->page;
        $responce->total = $this->total_pages;
        $responce->records = $this->cuont;
        $i = 0;
        foreach ($query->result() as $value) {
            $responce->rows[$i]['id'] = $value->proyectoId;
            $tmp = ($value->proyectoCelular == 0)?'':$value->proyectoCelular;
            $tmp1 = ($value->proyectoNumAvaluos == 0)?'':$value->proyectoNumAvaluos;
            $responce->rows[$i]['cell'] = array($value->proyectoContrato,$value->proyectoNombre, $value->proyectoDirector, $tmp, $value->proyectoTelefono, $value->proyectoDireccion, $tmp1, $value->proyectoObservacion, $value->proyectoEstado);
            $i++;
        }
        echo json_encode($responce);
    }//fin public function proyecto() {
    public function avaluos() {
        //Variables del jqgrid
        $this->cargaParametrosGrillaIniciales();
        $where = "where 1 = 1  " . $this->wh;
        $innerJoin = "inner join proyectos p on p.proyectoId = a.proyectoId";
        $sql = "select count(*) as total from avaluos a 
                $innerJoin ".$where;
        $obj = $this->db->query($sql)->result();
        $this->totalesGrilla($obj[0]->total);
        $sql = "select a.avaluoId,a.avaluoNumero,p.proyectoNombre,a.avaluoAprobado,a.avaluoObservacion from avaluos a $innerJoin $where ORDER BY " . $this->sidx . " " . $this->sord . " LIMIT " . $this->start . " , " . $this->limit;
        $responce = new stdClass();
        $query = $this->db->query($sql);
        $responce->page = $this->page;
        $responce->total = $this->total_pages;
        $responce->records = $this->cuont;
        $i = 0;
        foreach ($query->result() as $value) {
            $responce->rows[$i]['id'] = $value->avaluoId;
            $responce->rows[$i]['cell'] = array($value->proyectoNombre,$value->avaluoNumero,$value->avaluoAprobado,$value->avaluoObservacion);
            $i++;
        }
        echo json_encode($responce);
    }//fin public function avaluos() {
    public function consultaProyectos() {
        //Variables del jqgrid
        $this->cargaParametrosGrillaIniciales();
        $whereAux = ($this->input->post("proyectoEstado") && trim($this->input->post("proyectoEstado")) != "")?" and proyectoEstado = '{$this->input->post("proyectoEstado")}' ": "";
        $where = "where proyectoEstadoSistema = 'A'  ".$whereAux. $this->wh;
        $inner = " left join avaluos aval on aval.proyectoId = pro.proyectoId ";
        $sql = "select count(*) as total from proyectos pro $inner ".$where;
        $obj = $this->db->query($sql)->result();
        $this->totalesGrilla($obj[0]->total);
        $sql = "select pro.proyectoId,pro.proyectoContrato,pro.proyectoNombre,pro.proyectoEstado,concat(cast(count(aval.proyectoId) as char(100)),'/',cast(pro.proyectoNumAvaluos as char(100))) as avaluos  from proyectos pro ";
        $sql .= $inner. " $where group by pro.proyectoId ORDER BY " . $this->sidx . " " . $this->sord . " LIMIT " . $this->start . " , " . $this->limit;
        $query = $this->db->query($sql);
        $responce = new stdClass();
        $responce->page = $this->page;
        $responce->total = $this->total_pages;
        $responce->records = $this->cuont;
        $i = 0;
        foreach ($query->result() as $value) {
            $responce->rows[$i]['id'] = $value->proyectoId;
            $responce->rows[$i]['cell'] = array($value->proyectoContrato,$value->proyectoNombre, $value->proyectoEstado, $value->avaluos);
            $i++;
        }
        echo json_encode($responce);
    }//fin public function consultaProyectos() {
    public function avaluosAprobados($proyectoId,$avaluosId) {
        //Variables del jqgrid
        $this->cargaParametrosGrillaIniciales();
        $whereAux = (trim($proyectoId) != "")?" and proyectoId = '$proyectoId' ": "";
        $whereAux .= (trim($avaluosId) != "")?" and avaluoId = '$avaluosId' ": "";
        $where = "where 1 = 1  ".$whereAux. $this->wh;
        $sql = "select count(*) as total from avaluos ".$where;
        $obj = $this->db->query($sql)->result();
        $this->totalesGrilla($obj[0]->total);
        $sql = "select avaluoId,avaluoNumero,avaluoAprobado,avaluoObservacion  from avaluos $where ORDER BY " . $this->sidx . " " . $this->sord . " LIMIT " . $this->start . " , " . $this->limit;
        $query = $this->db->query($sql);
        $responce = new stdClass();
        $responce->page = $this->page;
        $responce->total = $this->total_pages;
        $responce->records = $this->cuont;
        $i = 0;
        foreach ($query->result() as $value) {
            $responce->rows[$i]['id'] = $value->avaluoId;
            $responce->rows[$i]['cell'] = array('',$value->avaluoNumero,$value->avaluoAprobado, $value->avaluoObservacion);
            $i++;
        }
        echo json_encode($responce);
    }//fin public function avaluosAprobados() {
    public function visitasAprobados($avaluosId) {
        //Variables del jqgrid
        $this->cargaParametrosGrillaIniciales();
        $where = "where avaluoId = $avaluosId  ". $this->wh;
        $sql = "select count(*) as total from visitas ".$where;
        $obj = $this->db->query($sql)->result();
        $this->totalesGrilla($obj[0]->total);
        $sql = "select visitaId,visitaVereda,visitaVeredaNombre,visitaFechaVisita,visitaFechaAvaluo,visitaFechaCreo  from visitas $where ORDER BY " . $this->sidx . " " . $this->sord . " LIMIT " . $this->start . " , " . $this->limit;
        $query = $this->db->query($sql);
        $responce = new stdClass();
        $responce->page = $this->page;
        $responce->total = $this->total_pages;
        $responce->records = $this->cuont;
        $i = 0;
        foreach ($query->result() as $value) {
            $responce->rows[$i]['id'] = $value->visitaId;
            $responce->rows[$i]['cell'] = array('',$value->visitaVereda,$value->visitaVeredaNombre, $value->visitaFechaVisita, $value->visitaFechaAvaluo, $value->visitaFechaCreo);
            $i++;
        }
        echo json_encode($responce);
    }//fin public function visitasAprobados() {
    public function avaluosVisitas() {
        //Variables del jqgrid
        $this->cargaParametrosGrillaIniciales();
        $avaluoId = $this->input->post("avaluoId");
        $inner = " inner join avaluos aval on aval.avaluoId = visi.avaluoId ";
        $where = "where visi.avaluoId = $avaluoId  " . $this->wh;
        $sql = "select count(*) as total from visitas visi ".$inner.$where;
        $obj = $this->db->query($sql)->result();
        $this->totalesGrilla($obj[0]->total);
        $sql = "select visi.visitaId,visi.visitaFechaVisita,aval.avaluoAprobado as visitaAprobado from visitas visi $inner $where ORDER BY " . $this->sidx . " " . $this->sord . " LIMIT " . $this->start . " , " . $this->limit;
        $query = $this->db->query($sql);
        $responce = new stdClass();
        $responce->page = $this->page;
        $responce->total = $this->total_pages;
        $responce->records = $this->cuont;
        $i = 0;
        foreach ($query->result() as $value) {
            $responce->rows[$i]['id'] = $value->visitaId;
            $responce->rows[$i]['cell'] = array($value->visitaFechaVisita,'', $value->visitaAprobado);
            $i++;
        }
        echo json_encode($responce);
    }//fin public function avaluosVisitas() {
    public function inveMercados($visitaId) {
        //Variables del jqgrid
        $this->cargaParametrosGrillaIniciales();
        $inner = " inner join topografias topo on topo.topografiasId = inve.topografiasId ";
        $where = "where topo.visitaId = $visitaId  " . $this->wh;
        $sql = "select count(*) as total from inveMercados inve ".$inner.$where;
        $obj = $this->db->query($sql)->result();
        $this->totalesGrilla($obj[0]->total);
        $sql = "select inve.inveMercadoId,topo.topografiaNombre, inve.inveMercadoInformante,inve.inveMercadoUbicacion,inve.inveMercadoCaractePredio,inve.inveMercadoValorOferta,inve.inveMercadoValorNegociar,inve.inveMercadoAreaTerreno,inve.inveMercadoAreaConstruccion,inve.inveMercadoValMetroConstruccion,inve.inveMercadoValorConstruccion,inve.inveMercadoValorTerreno,inve.inveMercadoValorMetroTerreno from inveMercados inve $inner $where ORDER BY " . $this->sidx . " " . $this->sord . " LIMIT " . $this->start . " , " . $this->limit;
        $query = $this->db->query($sql);
        $responce = new stdClass();
        $responce->page = $this->page;
        $responce->total = $this->total_pages;
        $responce->records = $this->cuont;
        $i = 0;
        foreach ($query->result() as $value) {
            $responce->rows[$i]['id'] = $value->inveMercadoId;
            $responce->rows[$i]['cell'] = array('', $value->topografiaNombre,$value->inveMercadoInformante,$value->inveMercadoUbicacion,$value->inveMercadoCaractePredio,$value->inveMercadoValorOferta,$value->inveMercadoValorNegociar,$value->inveMercadoAreaTerreno,$value->inveMercadoAreaConstruccion,$value->inveMercadoValMetroConstruccion,$value->inveMercadoValorConstruccion,$value->inveMercadoValorTerreno,$value->inveMercadoValorMetroTerreno);
            $i++;
        }
        echo json_encode($responce);
    }//fin public function inveMercados() {
    public function documentos($opc,$valorId) {
        //Variables del jqgrid
        $this->cargaParametrosGrillaIniciales();
        $inner = "";
        $campo = ($opc === "aval")?"avaluoId":"proyectoId";
        $where = "where $campo = $valorId  " . $this->wh;
        $sql = "select count(*) as total from documentosAvaluosProyectos ".$inner.$where;
        $obj = $this->db->query($sql)->result();
        $this->totalesGrilla($obj[0]->total);
        $sql = "select docAvalProId,docAvalProNombre,docAvalProRuta from documentosAvaluosProyectos $inner $where ORDER BY " . $this->sidx . " " . $this->sord . " LIMIT " . $this->start . " , " . $this->limit;
        $query = $this->db->query($sql);
        $responce = new stdClass();
        $responce->page = $this->page;
        $responce->total = $this->total_pages;
        $responce->records = $this->cuont;
        $i = 0;
        foreach ($query->result() as $value) {
            $responce->rows[$i]['id'] = $value->docAvalProId;
            $link = "<a href='".site_url("cargaDocumentos/descarga/{$value->docAvalProId}")."' target='_new'>Descargar</a>";
            $responce->rows[$i]['cell'] = array('', $value->docAvalProNombre,$link);
            $i++;
        }
        echo json_encode($responce);
    }//fin public function inveMercados() {
}

?>
